﻿//*** Guidance generated code ***//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ProjectBase.Core;

using $prjsn$.Common.Validation;

namespace $safeprojectname$
{
    public class RuleCollection<TItem> : IEnumerable<IValidationRule>
        where TItem : class
    {
        #region members
        readonly List<IValidationRule<TItem>> _rules = new List<IValidationRule<TItem>>();
        #endregion members

        #region Add, Remove
        public void Add(IValidationRule<TItem> item)
        {
            _rules.Add(item);
        }

        public void AddRange(IEnumerable<IValidationRule<TItem>> rules)
        {
            _rules.AddRange(rules);
        }

        public bool Remove(string propertyName)
        {
            var rules = _rules
                .Where(r => r.PropertyName.IsEqual(propertyName))
                .ToList();

            if (rules.IsEmpty())
            {
                return false;
            }
            foreach (var rule in rules)
            {
                _rules.Remove(rule);
            }
            return true;
        }

        #endregion Add, Remove

        public IEnumerable<IValidationRule<TItem>> Select(ValidateOn validateOn, ApplyOn applyOn)
        {
            return _rules
                .Where(r => (r.ValidateOn & validateOn).Equals(validateOn))
                .Where(r => (r.ApplyOn & applyOn).Equals(applyOn))
                .OrderBy(r => r.Order)
                ;
        }

        public IEnumerator<IValidationRule> GetEnumerator()
        {
            return _rules.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
